<?php
/**
 * Templates
 *
 *     SCMwiki Copyright (C) 2010  Callan Bryant <callan1990@googlemail.com>
 *
 *     Based on Voswork - A simple, fast PHP filesystem abstraction layer
 *     Voswork Copyright (C) 2010  Callan Bryant <callan1990@googlemail.com>
 *
 *     This program is free software: you can redistribute it and/or modify
 *     it under the terms of the GNU General Public License as published by
 *     the Free Software Foundation, either version 3 of the License, or
 *     (at your option) any later version.
 *
 *     This program is distributed in the hope that it will be useful,
 *     but WITHOUT ANY WARRANTY. without even the implied warranty of
 *     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *     GNU General Public License for more details.
 *
 *     You should have received a copy of the GNU General Public License
 *     along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *
 * @package main
 * @author Callan Bryant <callan1990@googlemail.com>
 */

/**
 * Instantiate and set attributes. Then echo.
 */
class template
{	
	// title of article
	public $title = null;
	// title of system
	public $system = SITE_NAME;
	// HTML5 article section. The main content
	public $article = null;
	// top comment, also used for meta tags
	public $description = null;
	// bottom note
	public $note = null;
	// text  to  display at bottom
	public $footer = 'Powered by <a target="_blank" href="http://scmwiki.googlecode.com/">scmwiki</a>';
	// comment to hide in source (eg, render time, cache type, any other debug stuff)
	public $comment = 'scmwiki';

	// TODO: Search focus thing all in separate, not embedded JS
	// returns page so ebject can be echoed
	public function __toString()
	{
		$page = '<!DOCTYPE html><html><head><title>'.$this->title.' - '.$this->system.'</title>'.
		'<link rel="stylesheet" type="text/css" href="'.CSS_URL.'" />';

		if ($this->description)
			$page .= '<meta name="description" content="'.$this->description.'" />';

		$page .= '</head><body><div id="banner"><h1>'.$this->system.'</h1>'.
		'</div><div id="main"><header><nav><form action="?node=search" method="post"><a href=".">Home</a> ';

		if (FEATURED_PAGES)
			foreach ( explode (',',FEATURED_PAGES) as $name)
				$page .= '<a href="'.parser::PAGE_LINK_PREFIX.$name.'">'.$name.'</a> ' ;

		$page .= '<input type="text" id="search" name="keywords" value="Search..." onFocus="this.value=\'\'" />'.
		'</form></nav><h1>'.$this->title.'</h1></header><article>'.
		'<div class="note">'.$this->description.'</div>';

		$page .= $this->article.'<div class="note" id="meta">'.$this->note.'</div>'.
		'</article></div><footer class="note">'.$this->footer.'</footer></body></html>'.
		// render time (etc)
		"\n<!--".$this->comment.'-->';

		return $page;
	}

	/**
	 * Given a number of seconds, it will return a rough worded delta such as
	 * 'a few moments'. The resolution changes accordingly with magnitude.
	 * @param int $seconds
	 * @return string human rough delta
	 */
	public static function evaluate_seconds($seconds)
	{
		// less than 2 minutes
		if ($seconds < 120)
			return 'moments';

		// less than an hour
		elseif ($seconds < 3600)
			return floor($seconds/60).' minutes';

		// less than 2 hours
		elseif ($seconds < 7200)
			return '1 hour';

		// less than a day
		elseif ($seconds < 86400)
			return floor($seconds/3600).' hours';

		// less than 2 days
		elseif ($seconds < 172800)
			return '1 day';

		// less than a week
		elseif  ($seconds < 592200)
			return floor($seconds/86400).' days';

		// less than 2 weeks
		elseif ($seconds < 1184400)
			return '1 week';

		// less than a month
		elseif  ($seconds < 2368800)
			return floor($seconds/592200).' weeks';

		// less than 2 months
		elseif ($seconds < 4737600)
			return '1 month';

		// less than a year
		elseif ($seconds < 30879000)
			return floor($seconds/2368800).' months';

		// less than 2 years
		elseif ($seconds < 61758000)
			return '1 year';

		// ...more than 2 years
		else
			return floor($seconds/30879000).' years';
	}
}
?>
